﻿using System;
using System.Threading;

using GameNetwork;
using GameNetwork.Interfaces;

namespace SlimGameClient
{
    public static class Program
    {
        private static IGameNetwork gameNetwork;

        private static IMenuNetwork menuNetwork;

        private static Thread gameThread;

        private static bool threadRun;

        public static void Main(string[] args)
        {
            GameClientRegistry.Register();
            menuNetwork = NetworkDistributor.GetMenuNetwork();
            gameNetwork = NetworkDistributor.GetGameNetwork();

            Console.WriteLine("Starting...");
            threadRun = true;
            gameThread = new Thread(Start);
            gameThread.Start();
            Console.WriteLine("Thread Started...");

            var key = Console.ReadKey();
            Console.WriteLine();

            while (key.Key != ConsoleKey.Escape)
            {
                if (key.Key == ConsoleKey.R)
                {
                    Console.WriteLine("Killing Thread...");
                    threadRun = false;

                    while (gameThread.IsAlive)
                    {
                        Thread.Sleep(100);
                    }
                    
                    Console.WriteLine("Closing Connection...");

                    threadRun = true;
                    Console.WriteLine("Restarting...");
                    gameThread = new Thread(Start);
                    gameThread.Start();
                }
                
                key = Console.ReadKey();
                Console.WriteLine();
            }

            threadRun = false;
        }


        private static void Start()
        {
            Console.WriteLine("Setup Connection...");
            menuNetwork.SetupConnection();

            Console.WriteLine("Register Player...");
            var result = menuNetwork.Register("FarmersBreakfast");

            if (!result)
            {
                Console.WriteLine("Connection Failed...");
                try
                {
                    menuNetwork.CancelRegistration();
                }
                catch (Exception)
                {
                }
                return;
            }
            
            while (!menuNetwork.IsGameReadyToLoad && threadRun)
            {
                Thread.Sleep(100);
            }

            if (!threadRun)
            {
                try
                {
                    gameNetwork.QuitGame();
                }
                catch (Exception)
                {
                }
                
                return;
            }

            Console.WriteLine("Game Ready to Load...");
            Console.WriteLine("Ready...");
            menuNetwork.Ready();

            while (!menuNetwork.IsGameStarted && threadRun)
            {
                Thread.Sleep(100);
            }

            if (!threadRun)
            {
                try
                {
                    gameNetwork.QuitGame();
                }
                catch (Exception)
                {
                }
                return;
            }

            Console.WriteLine("Game Started...");

            while (threadRun)
            {
                gameNetwork.GetGameState();
                Thread.Sleep(100);
            }

            try
            {
                gameNetwork.QuitGame();
            }
            catch (Exception)
            {
            }
        }
    }
}
